<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

class CreateOrdersTable extends Migration
{
	private $tableName = 'orders';
	private $tableComment = '订单表';
	private $primaryKey = 'id';

	/**
	 * Run the migrations.
	 *
	 * @return void
	 */
	public function up()
	{
		Schema::create($this->tableName, function (Blueprint $table) {
			$table->increments(env('DB_CONNECTION') == 'oracle' ? strtoupper($this->primaryKey) : $this->primaryKey)->comment('兑换id');
            $table->string('title', 50)->comment('众筹主题');
            $table->mediumText('openid')->comment('描述')->nullable();
            $table->string('username', 255)->comment('卖家者')->nullable();
            $table->unsignedTinyInteger('author_id')->comment('卖家id')->default(0);
            $table->unsignedTinyInteger('exchange_id')->comment('兑换商品id')->default(0);
            $table->unsignedTinyInteger('customer_id')->comment('客服id')->default(0);
            $table->unsignedTinyInteger('shop_id')->comment('店家id')->default(0);
            $table->unsignedTinyInteger('address_id')->comment('地址id')->default(0);
            $table->unsignedTinyInteger('good_id')->comment('商品id')->default(0);
            $table->unsignedTinyInteger('status')->comment('状态，1进行中,2待发货，3待评价4已完成')->default(1);
            
            $table->timestamps();

			if (env('DB_CONNECTION') == 'oracle') {
				$table->comment = $this->tableComment;
			}
		});

		if (env('DB_CONNECTION') == 'mysql') {
			DB::statement("ALTER TABLE `" . DB::getTablePrefix() . $this->tableName . "` comment '{$this->tableComment}'");
		}
	}

	/**
	 * Reverse the migrations.
	 *
	 * @return void
	 */
	public function down()
	{
		Schema::dropIfExists($this->tableName);
		if (env('DB_CONNECTION') == 'oracle') {
			$sequence = DB::getSequence();
			$sequence->drop(strtoupper($this->tableName . '_' . $this->primaryKey . '_SEQ'));
		}
	}
}
